define(
    function (require) {

        require('tpl!./list.tpl.html');
        var util = require('common/util');
        var globalData = require('common/GlobalData').getInstance();

        var cityList = [];
        var mallTree = globalData.getMallList();
        util.walkTree(mallTree, function (item) {if (item.type === 'city') {cityList.push(item);}});

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 40,
                content: function (item) {
                    return item.id;
                }
            },
            {
                title: '购物中心名称',
                field: 'name' ,
                resizable : true,
                width: 150,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="modify">' + item.name + '</a>';
                }
            },
            {
                title: '城市',
                field: 'cityname' ,
                sortable: true,
                resizable : true,
                width: 120,
                content: function (item) {
                    var city = util.find(cityList, function (city) {return city.value == item.cityId})
                    if (!util.isEmpty(city)) {
                        return city.text;
                    }
                    return '未知';
                }
            },
            {
                title: '地址',
                field: 'address' ,
                sortable: true,
                resizable : true,
                width: 300,
                content: function (item) {
                    if (!util.isEmpty(item.address)) {
                        return item.address.name;
                    }
                    return "未知";
                }
            },
            {
                title: '负责人电话',
                field: 'phone' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    if (util.isObject(item.chargePerson) && item.chargePerson.phone) {
                        return item.chargePerson.phone;
                    }
                    return '--'
                }
            },
            {
                title: '登录信息(用户名 / 密码)',
                field: 'login' ,
                sortable: true,
                resizable : true,
                width: 150,
                content: function (item) {
                    return item.username + ' / ' + item.password;
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 60,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="delete">删除</a>';
                }
            }
        ];

        function search() {
            var mallName = this.get('mallName');
            this.fire('search', { mallName: mallName.getValue() });
        }

        var BaseListView = require('common/ListView');

        function MallListView() {
            BaseListView.apply(this, arguments);
        }

        MallListView.prototype.template = 'mallListPage';

        MallListView.prototype.uiProperties = {
            list: {
                fields: fields,
                sortable: false,
                columnResizable: true,
                subrow: false,
                followHead: true,
                selectMode: 'line',
                noDataHtml: '<p style="margin:0;">暂无购物中心!</p>'
            }
        };

        MallListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );
            
            this.get('searchButton').on('click', util.bind(search, this));
        }

        require('er/util').inherits(MallListView, BaseListView);
        return MallListView;
    }
);
